package com.zwps.biz.domain.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwps.biz.api.enums.TrainEnum;
import com.zwps.biz.api.enums.ZAchievementEnum;
import com.zwps.biz.api.model.vo.fourpipes.*;
import com.zwps.biz.api.model.vo.pubpointall.DangerInfoDetailVO;
import com.zwps.biz.api.model.vo.pubpointall.HiddenDangerRecordVO;
import com.zwps.biz.api.model.vo.pubpointall.HiddenDangerVO;
import com.zwps.biz.api.model.vo.pubpointall.ZAchievementsOtherVO;
import com.zwps.biz.api.model.vo.strsvrr.RzWRelationVO;
import com.zwps.biz.api.model.vo.structure.SafetyAppraisalRecordVO;
import com.zwps.biz.api.model.vo.structure.ZGoodsMaterialsTypeVO;
import com.zwps.biz.api.model.vo.structure.ZGoodsMaterialsVO;
import com.zwps.biz.dal.db.dao.DsmDfrSrhrdsDAO;
import com.zwps.biz.dal.db.dao.VillageLandInfoDAO;
import com.zwps.biz.dal.db.dao.ZAchievementsDAO;
import com.zwps.biz.dal.db.object.VillageLandInfoDO;
import com.zwps.biz.dal.db.object.ZAchievementsDO;
import com.zwps.biz.dal.db.object.ZGoodsMaterialsDO;
import com.zwps.biz.domain.service.VillageLandInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 *
 */
@DS("one")
@Service
public class VillageLandInfoServiceImpl extends ServiceImpl<VillageLandInfoDAO, VillageLandInfoDO>
        implements VillageLandInfoService {
    @Autowired
    private VillageLandInfoDAO villageLandInfoDAO;
    @Autowired
    ZAchievementsDAO zAchievementsDAO;
    @Autowired
    DsmDfrSrhrdsDAO dsmDfrSrhrdsDAO;

    @Override
    public List<VillageLandInfoVO> getvillageLandInfo() {
        List<VillageLandInfoVO>  villageLan=new ArrayList<>();
        List<String> collect = villageLandInfoDAO.selectList(new LambdaQueryWrapper<VillageLandInfoDO>()
                .groupBy(VillageLandInfoDO::getVillage))
                .stream()
                .map(v -> v.getVillage())
                .collect(Collectors.toList());
        collect.stream().forEach(v -> {
            VillageLandInfoVO villageLandInfoVO = new VillageLandInfoVO();
            villageLandInfoVO.setVillage(v);
            villageLandInfoVO.setLandList(villageLandInfoDAO.getInfoList(v));
            villageLan.add(villageLandInfoVO);
        });

        return villageLan;
    }

    @Override
    public HiddenDangerCountVO getHiddenDangerCount(String year) {

        return villageLandInfoDAO.getHiddenDangerCount( year);
    }

    @Override
    public List<RzWRelationVO> getRzW() {
        return villageLandInfoDAO.getRzW();
    }

    @Override
    public TrainVO getTrain() {
        TrainVO train = villageLandInfoDAO.getTrain();
        List<TrainDetailVO> collect = villageLandInfoDAO.getTrainDetail().stream().map(v -> {
            v.setTypeName(TrainEnum.getByCode(v.getType()));
            return v;
        }).collect(Collectors.toList());
        train.setTrainDetailVOList(collect);
        return train;
    }

    @Override
    public List<PubCommonFileVO> getFileByType(Integer type) {
        return villageLandInfoDAO.getFileByType(type);
    }
    @Override
    public List<PubCommonVO> getFileByTypes() {
        List<PubCommonVO> fileByTypes = villageLandInfoDAO.getFileByTypes();
        List<PubCommonVO> collect = fileByTypes.stream().map(v -> {
            v.setChildren(villageLandInfoDAO.getFileByType(v.getId()));
            v.setNum(villageLandInfoDAO.countFileByTypes(v.getId()));
            return v;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public HiddenDangerVO getHiddenDanger(Integer type, String year) {
        HiddenDangerVO result = new HiddenDangerVO();
        List<HiddenDangerRecordVO> records = villageLandInfoDAO.getHiddenRecord(year);

        if (records != null && !records.isEmpty()) {
            // 1. 设置原始记录列表
            result.setHiddenDangerRecordList(records);

            // 2. 计算各状态数量
            long totalCount = records.size();
            long unprocessedCount = records.stream()
                    .filter(record -> record.getStatus() != null && record.getStatus() == 0)
                    .count();
            // 3. 设置统计字段
            result.setHiddenDangerCount((int) totalCount-(int)unprocessedCount);
            result.setHiddenDangerUnprocessedCount((int) unprocessedCount);
        } else {
            // 处理空数据情况
            result.setHiddenDangerCount(0);
            result.setHiddenDangerUnprocessedCount(0);
            result.setHiddenDangerRecordList(Collections.emptyList());
        }

        return result;
    }

    @Override
    public List<DangerInfoDetailVO> getAchievementCount(Integer timeYear) {
        List<DangerInfoDetailVO> list = new ArrayList<>();
        for (ZAchievementEnum value : ZAchievementEnum.values()) {
            DangerInfoDetailVO detailVO = new DangerInfoDetailVO();
            if (value.getValue() == 1) {

                detailVO.setType(value.getValue());

                // 使用 BigDecimal 来累加收入
                BigDecimal totalIncome = zAchievementsDAO.selectList(
                        new QueryWrapper<ZAchievementsDO>().lambda()
                                .eq(ZAchievementsDO::getType, value.getValue())
                                .eq(timeYear != null, ZAchievementsDO::getTimeYear, timeYear)
                ).stream()
                        .map(item -> {
                            String incomeStr = item.getIncome();
                            // 确保在收入为 null 或空时使用 BigDecimal.ZERO
                            return incomeStr != null && !incomeStr.isEmpty()
                                    ? new BigDecimal(incomeStr)
                                    : BigDecimal.ZERO;
                        })
                        .reduce(BigDecimal.ZERO, BigDecimal::add);

                detailVO.setNum(totalIncome);
                list.add(detailVO);
            }

        }
        List<DangerInfoDetailVO> date= zAchievementsDAO.getOterCount(timeYear);
        list.addAll(date);
        return list;

    }

    @Override
    public List<ZAchievementsDO> getAchievementsList(Integer type, Integer timeYear) {
        LambdaQueryWrapper<ZAchievementsDO> queryWrapper = new QueryWrapper<ZAchievementsDO>().lambda();
        if (type != null) {
            queryWrapper.eq(ZAchievementsDO::getType, type);
            queryWrapper.eq(timeYear != null, ZAchievementsDO::getTimeYear, timeYear);
        }
        List<ZAchievementsDO> zAchievementsDOS = zAchievementsDAO.selectList(queryWrapper);
        if (zAchievementsDOS == null ) {
            return new ArrayList<>();
        }
        zAchievementsDOS.stream().forEach(item -> {
            item.setTypeName(ZAchievementEnum.fromValue(item.getType()).getDescription());
        });
        return zAchievementsDOS;
    }

    @Override
    public List<String> getAchievementYear() {
        return zAchievementsDAO.getAchievementYear();
    }

    @Override
    public List<ZAchievementsDO> getAchievementsListGroupMonth(Integer type, Integer timeYear) {
        List<ZAchievementsDO> achievementsDOList = zAchievementsDAO.getAchievementsListGroupMonth(type, timeYear);
        if (timeYear != null) {
            for (ZAchievementsDO zAchievementsDO : achievementsDOList) {
                zAchievementsDO.setTypeName(ZAchievementEnum.fromValue(zAchievementsDO.getType()).getDescription());
            }
        }
        return achievementsDOList;
    }

    @Override
    public List<ZAchievementsOtherVO> getAchievementsOtherList(Integer type, Integer timeYear) {
        return zAchievementsDAO.getAchievementsOtherList(type,timeYear);
    }

    @Override
    public Map<String, Object> getPzwlSrhrdsCount() {
        return dsmDfrSrhrdsDAO.getPzwlSrhrdsCount();
    }

    @Override
    public List<SafetyAppraisalRecordVO> getSafetyAppraisalRecordList(Integer year) {
        return zAchievementsDAO.getSafetyAppraisalRecordList(year);
    }
}




